package com.android.demp;

import android.content.Context;
import android.database.*;
import android.database.sqlite.*;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public abstract class DBAdapter {
	protected static final String DATABASE_NAME = "vocDb.db";
	protected static final String DATABASE_TABLE = "doTable";
	protected static final int DATABASE_VERSION = 1;
	// The index (key) column name for use in where clauses.
	protected static final String KEY_ID = "_id";
	protected static final String KEY_WORD = "word";
	protected static final String KEY_SHORT_MEANING = "shortMeaning";
	protected static final String KEY_EXPLAIN = "explain";
	protected static final String KEY_IMAGE_PATH = "imagePath";
	protected static final String KEY_GRAMMAR = "grammar";
	
	// TODO: Create public field for each column in your table.
	// SQL Statement to create a new database.
	protected static final String DATABASE_CREATE_DONOTKNOW = "create table "
			+ DATABASE_TABLE + " (" + KEY_ID
			+ " integer primary key autoincrement, " +  KEY_WORD
			+ " text not null, " + KEY_SHORT_MEANING
			+ " text not null, " + KEY_EXPLAIN
			+ " text not null, " + KEY_IMAGE_PATH
			+ " text not null, " + KEY_GRAMMAR
			+ " text not null);";
	// Variable to hold the database instance
	protected SQLiteDatabase db;
	// Context of the application using the database.
	protected final Context context;
	// Database open/upgrade helper
	protected myDbHelper dbHelper;

	public DBAdapter(Context _context) {
		context = _context;
		
	}

	public DBAdapter open() throws SQLException {
		dbHelper = new myDbHelper(context, DATABASE_NAME, null,
				DATABASE_VERSION);
		if(dbHelper!=null)
		db = dbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		db.close();
	}

	
	protected static class myDbHelper extends SQLiteOpenHelper {
		public myDbHelper(Context context, String name, CursorFactory factory,
				int version) {
			super(context, name, factory, version);
		}

		// Called when no database exists in disk and the helper class needs
		// to create a new one.
		@Override
		public void onCreate(SQLiteDatabase _db) {
			
			_db.execSQL(DATABASE_CREATE_DONOTKNOW);
			
		}

		// Called when there is a database version mismatch meaning that the
		// version
		// of the database on disk needs to be upgraded to the current version.
		@Override
		public void onUpgrade(SQLiteDatabase _db, int _oldVersion,
				int _newVersion) {
			// Log the version upgrade.
			Log.w("TaskDBAdapter", "Upgrading from version " + _oldVersion
					+ " to " + _newVersion
					+ ", which will destroy all old data");
			// Upgrade the existing database to conform to the new version.
			// Multiple
			// previous versions can be handled by comparing _oldVersion and
			// _newVersion
			// values.
			// The simplest case is to drop the old table and create a new one.
			_db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			// Create a new one.
			onCreate(_db);
		}
	}
}